﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DonViNganh;
namespace HDVN5.Control
{
    public partial class ManageAccount : System.Web.UI.UserControl
    {
        private string user = "";
        private LoginModule.AccountModuleDataContext acc = new LoginModule.AccountModuleDataContext();
        private DonViNganhDataContext db = new DonViNganhDataContext();
        private Int32 maDonVi;
        private int cap;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                user = Request.QueryString["user"];
                TenTaiKhoanCu.Value = user;
                LoginModule.DANGKYSUDUNG dk = acc.DANGKYSUDUNGs.SingleOrDefault(p => p.TENTAIKHOAN.Trim().ToUpper() == user.Trim().ToUpper());
                NgayTao.Value = changeFromDateToString(dk.NGAYTAO);
                string dt = changeFromDateToString(dk.NGAYDANGNHAPLANDAU);
                NgaySua.Value = DateTime.Now.Date.ToShortDateString();
                NgayDangNhapLanDau.Value = dt;
                cap = Convert.ToInt32(Session["CAP"].ToString());
                maDonVi = Convert.ToInt32(Session["MADONVIID"].ToString());

                if (dt.Equals("0"))
                {
                    TaoCacSo(user, maDonVi, "A", cap);
                }

            }
        }

        private string changeFromDateToString(Nullable<DateTime> obj)
        {

            if (obj.HasValue)
            {
                return obj.Value.ToShortDateString();
            }
            else
            {
                return "0";
            }
        }

        #region DangNhapLanDauTien
        // Dang nhap dau tien tao cac bang doan pha, quan ly ma doan sinh, so len duong, so rung ru,so truong
        /// <summary>
        /// Doan nay tao cac so dau tien cho cac don vi
        /// </summary>
        /// <param name="tenTaiKhoanDangNhap"></param>
        /// <param name="madangnhap"></param>
        /// <param name="tentruong"></param>
        /// <param name="capDangNhap"></param>
        /// <param name="dangNhapLanDau"></param>
        private void TaoCacSo(string tenTaiKhoanDangNhap, Int32 madangnhap, string tentruong, int capDangNhap)
        {

            LoginModule.AccountModuleDataContext acc = new LoginModule.AccountModuleDataContext();
            LoginModule.DANGKYSUDUNG dk = acc.DANGKYSUDUNGs.SingleOrDefault(p => p.TENTAIKHOAN.ToUpper() == tenTaiKhoanDangNhap.ToUpper().Trim());

            if (dk != null)
            {
                if (dk.NGAYDANGNHAPLANDAU == null)
                {
                    if (capDangNhap == 1 || capDangNhap == 2 || capDangNhap == 4)
                    {
                        themNhungSoCuaCapDangNhapDaoLienDoan(madangnhap, tenTaiKhoanDangNhap, tentruong);
                    }
                    else
                    {
                        themNhungSoCuaDonViNganh(madangnhap, tenTaiKhoanDangNhap);
                    }
                }
            }

        }

        /// <summary>
        /// Doan nay them cac so cua dao va lien doan
        /// </summary>
        /// <param name="madangnhap"></param>
        /// <param name="tentaikhoan"></param>
        /// <param name="tentruong"></param>
        private void themNhungSoCuaCapDangNhapDaoLienDoan(Int64 madangnhap, string tentaikhoan, string tentruong)
        {

            db = new DonViNganhDataContext();
            SOTRUONG truong = new SOTRUONG();
            truong.IDDONVICAP = madangnhap;
            truong.NGAYLAP = DateTime.Now.Date;
            truong.NGAYTAO = DateTime.Now.Date;
            truong.TAIKHOANTAO = tentaikhoan;

            db.SOTRUONGs.InsertOnSubmit(truong);

            DONVI_SORUNG rung = new DONVI_SORUNG();
            rung.SORUNGSO = madangnhap;
            rung.MADONVIID = madangnhap;
            rung.NGAYLAPSO = DateTime.Now.Date;
            rung.NGAYTAO = DateTime.Now.Date;
            rung.TAIKHOANTAO = tentaikhoan;
            rung.HOTENTRUONG = tentruong;
            db.DONVI_SORUNGs.InsertOnSubmit(rung);

            DONVI_SOLENDUONG duong = new DONVI_SOLENDUONG();
            duong.SOLENDUONGSO = madangnhap;
            duong.MADONVIID = madangnhap;
            duong.NGAYLAPSO = DateTime.Now.Date;
            duong.NGAYTAO = DateTime.Now.Date;
            duong.TAIKHOANTAO = tentaikhoan;
            duong.HOTENTRUONG = tentruong;
            db.DONVI_SOLENDUONGs.InsertOnSubmit(duong);

            db.SubmitChanges();

        }

        /// <summary>
        /// Doan nay dang ky cac so cua don vi nganh
        /// </summary>
        /// <param name="madangnhap"></param>
        /// <param name="tentaikhoan"></param>
        private void themNhungSoCuaDonViNganh(Int32 madangnhap, string tentaikhoan)
        {

            QUANLYMADOANSINH quanlymadoansinh = new QUANLYMADOANSINH();
            quanlymadoansinh.MADONVIID = (int)madangnhap;
            quanlymadoansinh.NGAYCAPNHATMADOANSINH = DateTime.Now.Date;
            string ma = madangnhap.ToString();
            quanlymadoansinh.MADOANSINHHIENTAI = "XXXX" + ma + "000";
            db.QUANLYMADOANSINHs.InsertOnSubmit(quanlymadoansinh);

            db.SubmitChanges();


        }
        #endregion
    }
}